package com.wyb.service.impl;

import com.wyb.mapper.ArticleMapper;
import com.wyb.mapper.CommentMapper;
import com.wyb.mapper.UserMapper;
import com.wyb.pojo.Article;
import com.wyb.pojo.Comment;
import com.wyb.constant.Result;
import com.wyb.pojo.User;
import com.wyb.service.GetReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GetReplyServiceImpl implements GetReplyService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public Result getReply(int userId) {

//        List<Comment> comments = commentMapper.selectByUserId(userId);
//
//        System.out.println(comments);
//        List<Comment> comments1 = commentMapper.selectReplyByUserId(userId);
//        System.out.println(comments1);
//        if(comments.isEmpty()&&comments1.isEmpty()){
//            System.out.println("没有回复");
//            return new ResponseResult(201, "可能没有回复", null);
//        }
//
//        boolean b = comments.addAll(comments1);
//
//        //合并两种回复
//        List<Comment> collect = comments.stream().distinct().collect(Collectors.toList());
//
//        comments = collect;
//
//        //回复按时间降序
//        comments.sort(Comparator.comparing(Comment::getCreateTime).reversed());
//        if (b){
//            System.out.println("回复合并成功");
//        }

        List<Comment> comments = commentMapper.selectReplyByUserId(userId);

        List<Map<String,Object>> list = new ArrayList<>();
        int i = 0;
        for (Comment comment : comments) {
            i++;
            User user = userMapper.selectById(comment.getUserId());
            Article  article = articleMapper.selectById(comment.getTopicId());
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("nickName", user.getNickName());
            map.put("avatar", user.getAvatar());
            map.put("reply", comment);
            map.put("articleTitle", article.getArticleTitle());
            map.put("url", article.getOriginalUrl());
            map.put("articleId", article.getId());
            map.put("authorId", article.getUserId());
            list.add(map);
        }
        return new Result(200, "共有"+i+"条未读回复", list);
    }
}
